﻿#define Expection

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml;
using UmcProvider.Controls;
using UmcProvider;

namespace ChinaTelecom.SmartPhone.Controls
{
#if Expection   //if reserve mode
    public interface IComboBoxExpection : IUmc
    {
        string Name { get; }
        bool Enabled { get; }
        Size Size { get; }
        Point Location { get; }
        string Text { get; }
        Color BackColor { get; }
        Font Font { get; }
        Color ForeColor { get; }
    }
    public interface IComboBox : IComboBoxExtension, IComboBoxExpection { }
    [Designer(typeof(UmcDesigner<IComboBox>))]
#else                 //if exclude mode
    //Add the Properties to be excluded here
    [ExcludeProperty]
    public interface IComboBoxExculde :  IUmc
    {
    }
    public interface IComboBox : IComboBoxExtension, IComboBoxExculde { }
    [Designer(typeof(UmcDesigner<ComboBox>))]
#endif
    [ToolboxBitmap(typeof(System.Windows.Forms.ComboBox), "ComboBox.bmp")]
    public sealed class ComboBox : System.Windows.Forms.ComboBox, IComboBox
    {
    }

    //Add Extension Properties here , and Implement in Inheritance class
    public interface IComboBoxExtension
    {
    }

    //public interface IComboBox : IControl { }
    public sealed class UmcComboBox : DefaultConverter<UmcComboBox, ComboBox>, IComboBox
    {
        public override XmlNode[] Serialize(IComponent root, XmlDocument document)
        {
            XmlNode[] nodes = base.Serialize(root, document);

            XmlNode node = nodes.First();

            AppendList(document, node, root as ComboBox);

            return nodes;
        }

        public override IComponent DeSerialize(IDesignerHost host, XmlDocument document, XmlNode[] node)
        {
            IComponent component = base.DeSerialize(host, document, node);

            AddList(component as ComboBox, node.First());

            return component;
        }

        private void AddList(ComboBox comboBox, XmlNode node)
        {
            comboBox.DisplayMember = "Key";
            foreach (XmlNode item in node.ChildNodes)
            {
                comboBox.Items.Add(new KeyValuePair<string, string>(
                    item.Attributes["Text"].Value,
                    item.Attributes["Value"].Value));
            }
        }

        private void AppendList(XmlDocument xml, XmlNode node, ComboBox comboBox)
        {
            if (comboBox.Items.Count == 0)
                return;

            Type itemType = comboBox.Items[0].GetType();
            if (itemType == typeof(string))
            {
                foreach (string s in comboBox.Items)
                {
                    var item = node.AppendChild(xml.CreateNode(XmlNodeType.Element, "Item", null));
                    item.Attributes.Append(xml.CreateAttribute("Text")).Value = s;
                    item.Attributes.Append(xml.CreateAttribute("Value")).Value = s;
                }
            }
            else
            {
                foreach (KeyValuePair<string, string> s in comboBox.Items)
                {
                    var item = node.AppendChild(xml.CreateNode(XmlNodeType.Element, "Item", null));
                    item.Attributes.Append(xml.CreateAttribute("Text")).Value = s.Value;
                    item.Attributes.Append(xml.CreateAttribute("Value")).Value = s.Key;
                }
            }
        }

        #region Hidden
        public bool Enabled
        {
            get { throw new NotImplementedException(); }
        }

        public bool Visible
        {
            get { throw new NotImplementedException(); }
        }

        public System.Drawing.Size Size
        {
            get { throw new NotImplementedException(); }
        }

        public System.Drawing.Point Location
        {
            get { throw new NotImplementedException(); }
        }

        public string Text
        {
            get { throw new NotImplementedException(); }
        }

        public System.Drawing.Color BackColor
        {
            get { throw new NotImplementedException(); }
        }

        public System.Drawing.Font Font
        {
            get { throw new NotImplementedException(); }
        }

        public System.Drawing.Color ForeColor
        {
            get { throw new NotImplementedException(); }
        }
        public string Name
        {
            get { throw new NotImplementedException(); }
        }
        #endregion


    }
}
